package fr.enseirb.mycub.activity.component.layer;

import java.util.List;

import android.content.Intent;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import fr.enseirb.mycub.activity.MainActivity;
import fr.enseirb.mycub.activity.component.LocationServiceComponent;
import fr.enseirb.mycub.activity.component.layer.BikeLayer.BikePopulateOverlays;
import fr.enseirb.mycub.activity.component.layer.BikeLayer.BikeUpdateOverlays;
import fr.enseirb.mycub.component.map.OverlayBundle;
import fr.enseirb.mycub.db.DBAdapter;
import fr.enseirb.mycub.service.UpdateService;

public abstract class Layer {
	public OverlayBundle mStationsOverlay;
	
	protected DBAdapter mDBAdapter; 
	protected MainActivity activity;
	protected  MapView mMapView = null;
	protected LocationServiceComponent locationServiceComponent;
	protected  UpdateOverlays mUpdateOverlays = null;
	protected  PopulateOverlays mPopulateOverlays = null;
	
	public Layer(MainActivity activity, LocationServiceComponent locationServiceComponent,
			MapView mMapView, List<Overlay> mMapOverlays){
		this.activity = activity;
		mDBAdapter = DBAdapter
				.getInstance(activity);
		this.locationServiceComponent = locationServiceComponent;
		this.mMapView = mMapView;
		//setup update service
		Intent intent = new Intent(activity, UpdateService.class);
		intent.setAction(getIntentAdd());
		activity.startService(intent);
	}
	
	
	public abstract  void createPopulateOverlaysTaskAndExecute();

	public abstract void createUpdateOverlaysTaskAndExecute();
	
	public void removeOverlays(){
		mStationsOverlay.hideBalloon();
		mMapView.getOverlays().remove(mStationsOverlay);
	}
	
	public  void executePopulateOverlays() {
		if (mPopulateOverlays != null) {
			mPopulateOverlays.cancel(true);
		}
		createPopulateOverlaysTaskAndExecute();
	}

	public void executeUpdateOverlays() {
		if (mPopulateOverlays != null) {
			mPopulateOverlays.setUpdateOnPostExecute();
			return;
		}
		if (mUpdateOverlays != null) {
			mUpdateOverlays.setUpdateOnPostExecute();
		}
		createUpdateOverlaysTaskAndExecute();
	}
	
	public void onRemove(MainActivity activity){
		Intent intent = new Intent(activity, UpdateService.class);
		intent.setAction(getIntentDelete());
		activity.startService(intent);
	}


	protected abstract String getIntentAdd();
	protected abstract String getIntentDelete();
	
}
